//
//  CurrentData.m
//  Test passenger soldier
//
//  Created by ikuaiyue on 15/7/6.
//  Copyright (c) 2015年 shike. All rights reserved.
//

#import "CurrentData.h"
#define ID @"id"
#define PID @"pid"
#define PTYPE @"Ptype"
#define PNAME @"pname"
#define PALLTIME @"palltime"
#define PDATA @"pdata"
@implementation CurrentData
+(BOOL)checkTableCreatedInDb:(FMDatabase *)db
{
    
    NSString *createStr=@"CREATE  TABLE  IF NOT EXISTS 'zrsPTime' ('id' INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL  UNIQUE ,'pid' VARCHAR,'Ptype' VARCHAR ,'pname' VARCHAR ,'palltime' VARCHAR ,'pdata' VARCHAR)";
    BOOL worked = [db executeUpdate:createStr];
    FMDBQuickCheck(worked);
    return worked;
}

+(BOOL)saveData:(TimeModel *)model
{
    FMDatabase *db = [FMDatabase databaseWithPath:DATABASE_PATH];
    if (![db open]) {
        return NO;
    };
    
    [self checkTableCreatedInDb:db];
    
    NSString *insertStr=@"INSERT INTO 'zrsPTime' ('pid','Ptype','pname','palltime','pdata') VALUES (?,?,?,?,?)";
    BOOL worked;
    worked = [db executeUpdate:insertStr,model.pid,model.type,model.pname,model.palltime,model.pdata];
    [db close];
    return worked;
    
}

+(BOOL)uptateModel:(TimeModel *)model
{
    @synchronized (self){
        FMDatabase *db = [FMDatabase databaseWithPath:DATABASE_PATH];
        if (![db open]) {
            
            return NO;
        };
        BOOL worked = NO;
        worked = [db executeUpdate:@"update zrsPTime set palltime = ?    where pname = ? ",model.palltime,model.pname];
        if (worked) {
            [[NSNotificationCenter defaultCenter] postNotificationName:@"refeshDate" object:nil];
        }
        return worked;
        
    }
    

}
+(BOOL)uptateTypeModel:(TimeModel *)model
{
    @synchronized (self){
        FMDatabase *db = [FMDatabase databaseWithPath:DATABASE_PATH];
        if (![db open]) {
            
            return NO;
        };
        BOOL worked = NO;
        worked = [db executeUpdate:@"update zrsPTime set Ptype = ?    where pname = ? ",model.type,model.pname];
        if (worked) {
            [[NSNotificationCenter defaultCenter] postNotificationName:@"refeshDate" object:nil];
        }
        return worked;
        
    }
    
    
}
+(NSMutableArray*)fetchAll
{
    NSMutableArray *resultArr=[[NSMutableArray alloc]init];
    FMDatabase *db = [FMDatabase databaseWithPath:DATABASE_PATH];
    if (![db open]) {
        return resultArr;
    };
    [self checkTableCreatedInDb:db];
    FMResultSet *rs;
    rs=[db executeQuery:@"select * from zrsPTime"];
    while ([rs next]) {
        TimeModel *time=[[TimeModel alloc]init];
        time.pid = [rs stringForColumn:PID];
        time.pdata = [rs stringForColumn:PDATA];
        time.pname = [rs stringForColumn:PNAME];
        time.palltime = [rs stringForColumn:PALLTIME];
        time.type = [rs stringForColumn:PTYPE];
        [resultArr addObject:time];
    }
    [rs close];
    return resultArr;
}


+(NSMutableArray*)fetchAllFriendsFromLocal:(NSString *)pname
{
    NSMutableArray *resultArr=[[NSMutableArray alloc]init];
    FMDatabase *db = [FMDatabase databaseWithPath:DATABASE_PATH];
    if (![db open]) {
        return resultArr;
    };
    [self checkTableCreatedInDb:db];
    FMResultSet *rs;
    rs=[db executeQuery:@"select * from zrsPTime where pname = ? ",pname];
    while ([rs next]) {
        TimeModel *time=[[TimeModel alloc]init];
        time.pid = [rs stringForColumn:PID];
        time.pdata = [rs stringForColumn:PDATA];
        time.pname = [rs stringForColumn:PNAME];
        time.palltime = [rs stringForColumn:PALLTIME];
        [resultArr addObject:time];
    }
    [rs close];
    return resultArr;
}
+ (BOOL)insertData:(id)data toTable:(NSString *)name{
    @synchronized (self){
        FMDatabase *db = [FMDatabase databaseWithPath:DATABASE_PATH];
        if (![db open]) {
            return NO;
        };
        [self checkTableCreatedInDb:db];
        if ([data isKindOfClass:[NSDictionary class]]){
            [db open];
            
            NSMutableArray *s = [[NSMutableArray alloc] init];
            for (id value in [data allValues]){
                [s addObject:@"?"];
            }
            NSString *sql = [[NSString alloc] initWithFormat:@"INSERT OR REPLACE into %@ (%@) values (%@)", name, [[data allKeys] componentsJoinedByString:@","], [s componentsJoinedByString:@","]];
            BOOL result = [db executeUpdate:sql withArgumentsInArray:[data allValues]];
            [db close];
            return  result;
        }else {
            [db open];
            for (NSMutableDictionary *_data in data){
                NSMutableArray *s = [[NSMutableArray alloc] init];
                for (id value in [_data allValues]){
                    [s addObject:@"?"];
                }
                
                NSString *sql = [[NSString alloc] initWithFormat:@"INSERT OR REPLACE into %@ (%@) values (%@)", name, [[_data allKeys] componentsJoinedByString:@","], [s componentsJoinedByString:@","]];
                
                if (![db executeUpdate:sql withArgumentsInArray:[_data allValues]]){
                    NSLog(@"error");
                }
            }
            [db close];
            
            return YES;
        }
        return  NO;
    }
    
    
}

@end
